package com.ftg.learn.bus.sharecar_userecord.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ftg.learn.bus.sharecar_userecord.mapper.Sharecar_Userecord_Admin_Mapper;
import com.ftg.learn.bus.sharecar_userecord.pojo.sharecar_userecord_admin;
import com.ftg.learn.bus.sharecaruse_record.service.SharecarUseRecordService;
import com.ftg.learn.bus.util.ResultType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class Sharecar_Userecord_Admin_Service {
    @Autowired
    Sharecar_Userecord_Admin_Mapper suam;

    @Autowired
    SharecarUseRecordService scurs;



    /**
     * 查询全部
     * @return
     */
    public List<sharecar_userecord_admin> query(){
        return suam.query();
    }

    /**
     * 根据id查询一条
     * @param id
     * @return
     */
    public sharecar_userecord_admin queryOne(String id){
        if(id==null||id.equals("")){
            throw new RuntimeException("id不能为空");
        }
        return suam.selectById(id);
    }

    /**
     * 添加
     * @param sua
     * @return
     */
    public int insert(sharecar_userecord_admin sua){
        if(sua.getUsecar_id()==null || sua.getUsecar_id().equals("")){
            return -1;
        }
        scurs.SharecarUseInsertRecord(sua.getUsecar_id(),sua.getPrincipal());
        return suam.insert(sua);
    }

    /**
     * 修改
     * @param scura
     * @param usecar_id
     * @return
     */
    public int update(sharecar_userecord_admin scura,String usecar_id) {
        if(queryOne(usecar_id) == null || queryOne(usecar_id).equals("")){
            return -1;
        }
        QueryWrapper qw = new QueryWrapper();
        qw.eq("usecar_id",usecar_id);
        scurs.SharecarUseUpdateRecord(usecar_id,scura.getPrincipal());
        return suam.update(scura,qw);
    }

    /**
     *删除
     */
    public  int delete(String id){
        return suam.deleteById(id);
    }

    /**
     * 分页查询
     * @param page        --页数
     * @param limit       --每页显示条目数
     * @param usecar_id   --实体类主键  用于模糊查询
     * @param principal   --负责人     用于模糊查询
     * @return
     */
    public ResultType<sharecar_userecord_admin> query(int page, int limit,String usecar_id,String principal) {
        ResultType<sharecar_userecord_admin> rs = new ResultType<>();
        rs.setCode(0);
        IPage<sharecar_userecord_admin> ip = new Page<>(page,limit);
        //给语句添加条件
        QueryWrapper<sharecar_userecord_admin> qw=new QueryWrapper<>();
        //多and拼接
        qw.and(wrapper->wrapper.like("usecar_id",usecar_id).like("principal",principal));
        ip =  suam.selectPage(ip,qw);
        rs.setData(ip.getRecords());
        rs.setCount(ip.getTotal());
        return rs;
    }

}



